
<template>
  <div class="jeepay-m-center" style="margin-top: 5px; margin-left: 20px;">
    <a-steps v-show="(vdata.showStyle == showStyleEnum.step) && !isMobile" :current="vdata.currentStep - 1" type="navigation">
      <a-step title="主体信息" />
      <a-step title="经营信息" />
      <a-step title="结算账户" />
    </a-steps>
    <a-divider v-show="!isMobile" />
    <a-divider v-show="isMobile" class="jeepay-m-divider" orientation="left" style="color: #1A66FF;"><span style="color: #2980FD;">{{ vdata.currentStep }}</span>/{{ vdata.steps.length }} {{ vdata.steps[vdata.currentStep - 1] }}</a-divider>
    <!-- 1. 主体信息  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 1"
      ref="stepForm1Ref"
      style="margin-top: 20px;"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 8 }"
      :rules="vdata.step1Rules"
    >
      <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">基础项</a-divider>
      <a-form-item label="商户类型" name="merchantType">
        <a-radio-group v-model:value="applymentDetailInfo.merchantType" @change="searchMchType(applymentDetailInfo.merchantType)">
          <a-radio :value="1">个人</a-radio>
          <a-radio :value="2">个体工商户</a-radio>
          <a-radio :value="3">企业</a-radio>
        </a-radio-group>
      </a-form-item>
      
      <a-form-item label="机构类型" name="acqOrgType">
        <a-select v-model:value="applymentDetailInfo.acqOrgType" placeholder="机构类型">
          <a-select-option value="RYX">瑞银信</a-select-option>
          <a-select-option value="ZF">中付</a-select-option>
          <a-select-option value="COMMONS">其他</a-select-option>
        </a-select>
      </a-form-item>

      <div v-if="applymentDetailInfo.merchantType != 1">
        <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">营业执照</a-divider>

        <a-form-item label="营业执照照片" name="licenseImg">
          <JeepayUpload v-model:src="applymentDetailInfo.licenseImg" bizType="applyment" ocrType="license" @ocrScan="ocrScanFunc" />
          <span class="jeepay-tip-text">(上传图片自动识别 营业执照编号)</span>
        </a-form-item>

        <a-form-item label="营业执照编号" name="licenseNo">
          <a-input v-model:value.trim="applymentDetailInfo.licenseNo" />
        </a-form-item>

        <a-form-item class="jeepay-valid" label="营业执照有效期截止" name="licenseEffectEnd">
          <a-input v-model:value.trim="applymentDetailInfo.licenseEffectEnd" :disabled="applymentDetailInfo.licenseEffectEnd == '长期'" style="width: 150px;margin-right: 10px;" />
          <a-checkbox :checked="applymentDetailInfo.licenseEffectEnd == '长期' " @change="(e) => applymentDetailInfo.licenseEffectEnd = e.target.checked ? '长期' : '' ">长期</a-checkbox>
        </a-form-item>
      </div>
        
      <a-form-item label="商户名称" name="mchFullName">
        <a-input v-model:value.trim="applymentDetailInfo.mchFullName" />
      </a-form-item>

      <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">法定代表人证件</a-divider>

      <a-form-item label="法人身份证人像面照片" name="idcard1Img">
        <JeepayUpload v-model:src="applymentDetailInfo.idcard1Img" bizType="applyment" ocrType="idCard" @ocrScan="ocrScanFunc" />
        <span class="jeepay-tip-text">(上传图片自动识别 身份证名称 身份证号)</span>
      </a-form-item>

      <a-form-item label="法人身份证国徽面照片" name="idcard2Img">
        <JeepayUpload v-model:src="applymentDetailInfo.idcard2Img" bizType="applyment" ocrType="idCard" @ocrScan="ocrScanFunc" />
      </a-form-item>

      <a-form-item label="法人身份证姓名" name="idcardName">
        <a-input v-model:value.trim="applymentDetailInfo.idcardName" />
      </a-form-item>

      <a-form-item label="法人身份证号" name="idcardNo">
        <a-input v-model:value.trim="applymentDetailInfo.idcardNo" />
      </a-form-item>
    </a-form>

    <!-- 2. 经营信息  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 2"
      ref="stepForm2Ref"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 8 }"
      :rules="vdata.step2Rules"
    >
      <a-form-item label="经营类目（MCC）" name="mccCode">
        <JeepayYsfMccSelect v-model:value="applymentDetailInfo.mccCode" />
      </a-form-item>

      <a-form-item label="商户联系人姓名" name="contactName">
        <a-input v-model:value.trim="applymentDetailInfo.contactName" />
      </a-form-item>

      <a-form-item label="商户联系人手机号" name="contactPhone">
        <a-input v-model:value.trim="applymentDetailInfo.contactPhone" />
      </a-form-item>

      <a-form-item label="客服电话" name="serviceTel">
        <a-input v-model:value.trim="applymentDetailInfo.serviceTel" />
      </a-form-item>

      <a-form-item label="邮箱地址" name="contactEmail">
        <a-input v-model:value.trim="applymentDetailInfo.contactEmail" />
      </a-form-item>

      <a-form-item label="商户经营省市县" name="areaCode">
        <JeepayAreaSelectYsf v-model:value="applymentDetailInfo.areaCode" @change="searchAreaFunc" />
      </a-form-item>

      <a-form-item label="商户经营地址" name="address">
        <a-input v-model:value.trim="applymentDetailInfo.address" />
      </a-form-item>
    
      <a-form-item label="门头照" name="storeOuterImg">
        <JeepayUpload v-model:src="applymentDetailInfo.storeOuterImg" bizType="applyment" />
      </a-form-item>

      <a-form-item label="门店外景照" name="storeOuter2Img">
        <JeepayUpload v-model:src="applymentDetailInfo.storeOuter2Img" bizType="applyment" />
      </a-form-item>

      <a-form-item label="店内环境照" name="storeInnerImg">
        <JeepayUpload v-model:src="applymentDetailInfo.storeInnerImg" bizType="applyment" />
      </a-form-item>
      
      <a-form-item label="收银台照" name="storeCashierImg">
        <JeepayUpload v-model:src="applymentDetailInfo.storeCashierImg" bizType="applyment" />
      </a-form-item>
    </a-form>

    <!-- 3. 结算账户  -->
    <a-form
      v-show="vdata.showStyle == showStyleEnum.block || vdata.currentStep == 3"
      ref="stepForm3Ref"
      :model="applymentDetailInfo"
      :label-col="{ span: 4 }"
      :wrapper-col="{ span: 8 }"
      :rules="vdata.step3Rules"
    >
      <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">账户信息</a-divider>
      <a-form-item label="账户类型" name="settleAccountType">
        <a-radio-group v-model:value="applymentDetailInfo.settleAccountType" :disabled="applymentDetailInfo.merchantType == '1'" @change="() => emit('itemRender')">
          <a-radio value="1">对公账户</a-radio>
          <a-radio value="2">法人账户</a-radio>
          <a-radio value="3">授权人账户</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="结算周期" name="settlePeriod">
        <a-radio-group v-model:value="applymentDetailInfo.settlePeriod" @change="() => emit('itemRender')">
          <a-radio value="1">T+1</a-radio>
          <a-radio value="2">D+0</a-radio>
          <a-radio value="3">T+0</a-radio>
          <a-radio value="4">D+1</a-radio>
        </a-radio-group>
      </a-form-item>

      <a-form-item label="银行卡正面照" name="settAccountLicenseImg">
        <JeepayUpload v-model:src="applymentDetailInfo.settAccountLicenseImg" ocrType="bankCard" bizType="applyment" @ocrScan="ocrScanFunc" />
        <span class="jeepay-tip-text">(上传图片自动识别 银行卡号)</span>
      </a-form-item>

      <a-form-item label="银行开户名" name="settAccountName">
        <a-input v-model:value.trim="applymentDetailInfo.settAccountName" />
        <span class="jeepay-tip-text">如：张三</span>
      </a-form-item>
      
      <a-form-item label="银行预留手机号" name="openBankReservePhone">
        <a-input v-model:value.trim="applymentDetailInfo.openBankReservePhone" />
      </a-form-item>

      <a-form-item label="银行账号" name="settAccountNo">
        <a-input v-model:value.trim="applymentDetailInfo.settAccountNo" />
      </a-form-item>

      <a-form-item label="银行名称" name="settAccountBankName">
        <a-input v-model:value.trim="applymentDetailInfo.settAccountBankName" />
      </a-form-item>
      
      <a-form-item label="开户支行名称" name="settAccountBankBranchName">
        <a-input v-model:value.trim="applymentDetailInfo.settAccountBankBranchName" />
      </a-form-item>

      <a-form-item label="联行行号" name="openBankCode">
        <a-input v-model:value.trim="applymentDetailInfo.openBankCode" />
        <span><a href="https://www.icvio.cn/" target="_blank">联行号及开户支行名称查询</a></span>
      </a-form-item>

      <div v-if="applymentDetailInfo.settleAccountType == 3">
        <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">结算授权人信息</a-divider>

        <a-form-item label="被授权人证件人像照" name="settAccountIdcard1Img">
          <JeepayUpload v-model:src="applymentDetailInfo.settAccountIdcard1Img" bizType="applyment" ocrType="idCard" @ocrScan="ocrScanFunc2" />
          <span class="jeepay-tip-text">(上传图片自动识别 身份证号)</span>
        </a-form-item>

        <a-form-item label="被授权人证件国徽照" name="settAccountIdcard2Img">
          <JeepayUpload v-model:src="applymentDetailInfo.settAccountIdcard2Img" bizType="applyment" />
        </a-form-item>

        <a-form-item label="被授权人身份证号" name="settAccountIdcardNo">
          <a-input v-model:value.trim="applymentDetailInfo.settAccountIdcardNo" />
        </a-form-item>

        <a-form-item label="结算授权证书" name="settleAuthLetterPhoto">
          <JeepayUpload v-model:src="applymentDetailInfo.settleAuthLetterPhoto" bizType="applyment" />
        </a-form-item>
      </div>

      <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">补充资料</a-divider>
      <a-form-item v-if="applymentDetailInfo.settleAccountType == 1" label="开户许可证" name="companyAccountLicenseImg">
        <JeepayUpload v-model:src="applymentDetailInfo.companyAccountLicenseImg" bizType="applyment" />
      </a-form-item>

      <a-form-item label="商户收单协议" name="acquiringAgreementPhoto">
        <JeepayUpload v-model:src="applymentDetailInfo.acquiringAgreementPhoto" bizType="applyment" />
      </a-form-item>

      <a-form-item label="拓展资料" name="reserve1">
        <JeepayUpload v-model:src="applymentDetailInfo.reserve1" bizType="applyment" />
      </a-form-item>

      <a-divider class="jeepay-m-divider" orientation="left" style="color: #1A66FF;">产品费率</a-divider>

      <div label="配置费率" name="fee" style="width:100%;">
        <JeepayPaywayRatePanel 
          ref="jeepayPaywayRatePanelRef" 
          :configMode="configMode"
          :infoId=" (mchApplymentData.applyId || '') + '_' + mchApplymentData.mchNo" 
          :selectIfCode="mchApplymentData.ifCode" 
          onlyRate="true"
        />
      </div>
    </a-form>
    <a-divider />

    <div class="jeepay-btn-box jeepay-m-btn-box" style="margin-top: 50px; margin-bottom: 50px;text-align: center;">
      <a-button v-show="vdata.showStyle == showStyleEnum.step && vdata.currentStep > 1" class="jeepay-m-btn" data-type="inline" type="primary" @click="toPrevStep"><left-circle-outlined /> 上一步 </a-button>
      <a-button v-show="vdata.showStyle == showStyleEnum.step && vdata.currentStep < 3" class="jeepay-m-btn" data-type="inline" type="primary" @click="toNextStep">下一步 <right-circle-outlined /> </a-button>
    </div>
  </div>
</template>

<script setup lang="ts">

import {reactive, getCurrentInstance, defineEmits, inject, ref} from 'vue'
import ruleGenerator from '@/utils/ruleGenerator'

const jeepayPaywayRatePanelRef = ref()

// 获取当前实例的代理对象
const { proxy } : any = getCurrentInstance()

const { $infoBox } = getCurrentInstance()!.appContext.config.globalProperties


// 显示类型枚举值
const showStyleEnum = {
  step: 'step',
  block: 'block'
}

// emit： 向父组件进行通讯。 
const emit = defineEmits(['itemRender'])

// 参数注入： 是否手机端
let isMobile : any = inject('isMobile')

// 响应式数据
const vdata : any = reactive({
  showStyle: 'step', // 显示类型： step-分步,  block-整页显示 
  steps: ['主体信息', '经营信息', '结算账户'],
  isTempData: false, // 是否保存草稿
  isvDefaultPaywayFee: {} as any, // 服务商默认费率
  currentStep: 1,
  receiverTableData: [] as any,
  step1Rules: {
                merchantType: [ ruleGenerator.requiredSelect('商户类型', 'number') ],
                acqOrgType: [ ruleGenerator.requiredSelect('机构类型', 'string') ],
                licenseImg: [ ruleGenerator.requiredUpload('营业执照照片') ],
                licenseNo: [ ruleGenerator.requiredInput('营业执照编号') ],
                licenseEffectEnd: [ ruleGenerator.requiredInput('营业执照截止有限期'), ruleGenerator.dateOrForever ],
                mchFullName: [ ruleGenerator.requiredInput('商户名称') ],
                idcard1Img: [ ruleGenerator.requiredUpload('法人身份证人像面') ],
                idcard2Img: [ ruleGenerator.requiredUpload('法人身份证国徽面') ],
                idcardName: [ ruleGenerator.requiredInput('身份证姓名') ],
                idcardNo: [ ruleGenerator.requiredInput('身份证号') ],
              },
  step2Rules: {
                mccCode: [ ruleGenerator.requiredSelect('行业') ],
                mchShortName: [ ruleGenerator.requiredInput('商户简称') ],
                contactName: [ ruleGenerator.requiredInput('联系人姓名') ],
                contactPhone: [ ruleGenerator.requiredInput('联系人电话') ],
                serviceTel: [ ruleGenerator.requiredInput('客服电话') ],
                contactEmail: [ ruleGenerator.requiredInput('邮箱地址') ],
                areaCode: [ ruleGenerator.requiredSelect('省市区', 'array') ],
                address: [ ruleGenerator.requiredInput('经营地址') ],
                storeOuterImg: [ ruleGenerator.requiredUpload('门头照') ],
                storeOuter2Img: [ ruleGenerator.requiredUpload('门店外景照') ],
                storeInnerImg: [ ruleGenerator.requiredUpload('门店内景照') ],
                storeCashierImg: [ ruleGenerator.requiredUpload('收银台照') ],
  },
  step3Rules: {
                settleAccountType: [ ruleGenerator.requiredSelect('账户类型') ],
                settlePeriod: [ ruleGenerator.requiredSelect('结算周期') ],
                settAccountNo: [ ruleGenerator.requiredInput('银行帐号') ],
                openBankReservePhone: [ ruleGenerator.requiredInput('预留手机号') ],
                settAccountLicenseImg: [ ruleGenerator.requiredUpload('银行卡正面照') ],
                settAccountName: [ ruleGenerator.requiredInput('开户名') ],
                settAccountBankName: [ ruleGenerator.requiredInput('银行名称') ],
                settAccountBankBranchName: [ ruleGenerator.requiredInput('开户支行名称') ],
                openBankCode: [ ruleGenerator.requiredInput('联行行号') ],
                settAccountIdcard1Img: [ ruleGenerator.requiredUpload('被授权人身份证人像照') ],
                settAccountIdcard2Img: [ ruleGenerator.requiredUpload('被授权人身份证国徽照') ],
                settAccountIdcardNo: [ ruleGenerator.requiredInput('被授权人身份证号') ],
                settleAuthLetterPhoto: [ ruleGenerator.requiredUpload('结算授权证书') ],
                companyAccountLicenseImg: [ ruleGenerator.requiredUpload('开户许可证') ],
                acquiringAgreementPhoto: [ ruleGenerator.requiredUpload('商户收单协议') ],
  }
  
})

// 接收进件参数（包含 商户号， ifCode ）
let mchApplymentDataRef : any = inject('mchApplymentData')
let mchApplymentData : any = mchApplymentDataRef.value

// 接收父组件的进件详细参数
let applymentDetailInfoInject : any = inject('applymentDetailInfo')
let applymentDetailInfo : any = applymentDetailInfoInject.value

// 参数注入： 配置方式
let configMode : any = inject('configMode')


// 下一步
function toNextStep() {
  // 验证是否通过
  proxy.$refs['stepForm'+vdata.currentStep+'Ref'].validate().then( () => {
      vdata.currentStep ++
  })
}

// 上一步
function toPrevStep(){
  vdata.currentStep --
}

function validateAllForm(step: number){
  return proxy.$refs['stepForm'+step+'Ref'].validate().then( () => {
     if(step >= 3){
      return Promise.resolve()
     }
     return validateAllForm(++step)
     
  })
}

if(applymentDetailInfo.feeData) {
  vdata.receiverTableData = applymentDetailInfo.feeData
}

// 图片识别的回调函数
function ocrScanFunc(orcObject: any){
  // 商户地址不赋值
  orcObject.address = ''

  if(orcObject){
    Object.assign(applymentDetailInfo, orcObject)
  }
}
// 图片识别的回调函数
function ocrScanFunc2(orcObject: any){
  if(orcObject){
    applymentDetailInfo.settAccountIdcardNo = orcObject.idcardNo
  }
}

// 点击【保存】的前置事件, 需要返回Promise对象
function saveDataPreCallback(isTemp){

  // 放置费率配置项
  let paywayFeeList = jeepayPaywayRatePanelRef.value.getReqMchRatePaywayFeeList()
  if(typeof(paywayFeeList) != 'object'){
      return Promise.reject()
  }

  // 赋值到对象中
  applymentDetailInfo.paywayFeeList = paywayFeeList


  // 站点信息
  applymentDetailInfo.feeData = vdata.receiverTableData
  // 保存草稿只校验当前步骤数据
  if (isTemp) {
    return Promise.resolve()
  }

  // 验证所有表单
  return validateAllForm(1).then(() => {
    return Promise.resolve()
  }).catch((params: any) => {
    $infoBox.message.error('数据填写不完整， 请检查后提交')
    return Promise.reject()
  })
}


// 省市区选择回调
function searchAreaFunc(value) {
  if (applymentDetailInfo.address) {
    applymentDetailInfo.address = ''
  }
  applymentDetailInfo.address = value[0].label + '-' + value[1].label + '-' + value[2].label + '-'
}

function searchMchType(value) {
  if(value == '1') {
    applymentDetailInfo.settleAccountType = '2'
  }
}

defineExpose({ saveDataPreCallback })

</script>

<style lang="less" scoped>
.jeepay-tip-text{
  font-size: 10px; color: rebeccapurple
}
.option-left {
  width: 50%;
  word-wrap: break-word; 
  white-space: normal;
  word-break: break-all;
}
.option-right {
  color: rgb(153, 153, 153);
}
.option-bottom {
  word-wrap: break-word; 
  white-space: normal;
  word-break: break-all;
  color: rgb(153, 153, 153);
}
.jeepay-btn-box {
  margin: 15px -12px !important;
}
.jeepay-btn-box .ant-btn {
  margin: 0 10px;
}
</style>